package com.sfh.cp.home.pst;


import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.sfh.cp.AppContext;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import crm.sfh.server.Const;
import crm.sfh.server.MangeNote;
import crm.sfh.server.MangerUser;
import crm.sfh.server.SNote;
import crm.sfh.server.SUser;
import crm.sfh.server.entity.EContent;
import crm.sfh.server.entity.ENote;
import crm.sfh.server.entity.EUser;
import crm.sfh.server.entity.req.ReqPage;
import crm.sfh.server.entity.rsp.TPage;

public class PstCity extends AbstractPresenter<IPstCity.IVCity> implements IPstCity {


    @Override
    public void listPage(final ReqPage req) {

        SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        if (TextUtils.isEmpty(user.getObjectId())) {
            if (TextUtils.isEmpty(AppCacheManager.newInstance().getCache(Const.CITY, ""))) {
                AppContext.getLocationListener(AppCacheManager.getApplication(), new AMapLocationListener() {
                    @Override
                    public void onLocationChanged(AMapLocation amapLocation) {
                        if (amapLocation != null && amapLocation.getErrorCode() == 0) {
                            //可在其中解析amapLocation获取相应内容。
                            AppCacheManager.newInstance().putCache(false, Const.CITY, amapLocation.getCity());
                            page(amapLocation.getCity(), req);
                        }
                    }
                });
            } else {
                page(AppCacheManager.newInstance().getCache(Const.CITY, ""), req);
            }
        } else {
            page(user.getAddress(), req);
        }

    }

    private void page(String city, final ReqPage req) {
        BmobQuery<SNote> query = new BmobQuery<>();

        // 根据score字段降序显示数据
        query.addWhereEqualTo("city", city);
        query.order("-sizeReading");
        query.setSkip((req.nowPage - 1) * 10);
        query.setLimit(req.nowPage * 10);
        query.include("author,fristMsg.author,twoMsg.author,threeMsg.author");
        query.findObjects(AppCacheManager.getApplication(), new FindListener<SNote>() {
            @Override
            public void onSuccess(List<SNote> list) {

                TPage<EContent> page = new TPage<>();
                page.setNowPage(req.nowPage);
                page.setTotalPage(list.size() >= 10 ? req.nowPage + 1 : req.nowPage);
                List<EContent> contents = new ArrayList<>(10);
                //更新阅览次数
                new MangeNote().updateReadSize(AppCacheManager.getApplication(), list);
                for (SNote note : list) {
                    EContent content = new EContent();
                    content.setContentType(note.getENote());
                    contents.add(content);
                }
                page.setList(contents);
                getView().onSuccess(page);
            }

            @Override
            public void onError(int i, String s) {
                getView().onError("未查询到数据");
            }
        });
    }

    @Override
    public void updateFabulous(ENote req) {
        new MangeNote().updateFabulous(AppCacheManager.getApplication(), req.getNoteId(), 1, null);
        // 更新魅力+1
        new MangerUser().updateCharm(AppCacheManager.getApplication(), req.getAuthor().getUserId(), 1, null);
    }

}